/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientsPan.java
 *
 * Created on 4 juil. 2011, 19:40:12
 */
package app.views;

import app.Tools;
import app.controllers.Registre;
import app.core.IncludedPan;
import app.core.UpdatedComp;
import app.events.ClientListener;
import app.events.ClientSelectedListener;
import app.events.eventobjects.ClientAddedEvent;
import app.events.eventobjects.ClientDeletedEvent;
import app.events.eventobjects.ClientUpdatedEvent;
import app.models.Client;
import app.models.Factory;
import java.awt.Color;
import java.awt.Cursor;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JComponent;
import javax.persistence.Query;

/**
 *
 * @author Ghost
 */
public class ClientsPan extends javax.swing.JPanel implements IncludedPan, ClientSelectedListener {
    
    Registre registre;
    
    //client being to be edited
    private Client editingClient;

    /** Creates new form ClientsPan */
    public ClientsPan() {
        initComponents();
    }

    public void setRegistre(Registre registre) {
        this.registre = registre;
        registre.addClientListener(clientsTable);
    }

    @Override
    public String getName(){
        return "Registre";
    }

    @Override
    public JComponent getComponent() {
        return this;
    }
    
    @Override
    public void load(UpdatedComp comp) {
        comp.setText("##Chargement des clients");
        registre.load(comp);
        comp.setText("##Fin chargement des clients");
        
        //clientsTable.setClients(registre.getClients());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addPan = new javax.swing.JPanel();
        lblNom = new javax.swing.JLabel();
        txtNom = new javax.swing.JTextField();
        txtPrenom = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        lblTel = new javax.swing.JLabel();
        txtTel = new javax.swing.JTextField();
        lblEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        btOk = new javax.swing.JButton();
        btVider = new javax.swing.JButton();
        clientsTable = new app.views.ClientsGui();

        FormListener formListener = new FormListener();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(300, 230));

        addPan.setBorder(javax.swing.BorderFactory.createTitledBorder("Nouveau Fidèle"));
        addPan.setPreferredSize(new java.awt.Dimension(200, 100));

        lblNom.setText("Nom:");

        jLabel2.setText("Prénom:");

        lblTel.setText("Tel:");

        lblEmail.setText("Email:");

        btOk.setText("Ajouter");
        btOk.addActionListener(formListener);

        btVider.setText("Vider");
        btVider.setBorderPainted(false);
        btVider.setContentAreaFilled(false);
        btVider.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btVider.addActionListener(formListener);

        javax.swing.GroupLayout addPanLayout = new javax.swing.GroupLayout(addPan);
        addPan.setLayout(addPanLayout);
        addPanLayout.setHorizontalGroup(
            addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addPanLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addPanLayout.createSequentialGroup()
                        .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(addPanLayout.createSequentialGroup()
                                .addComponent(lblNom)
                                .addGap(7, 7, 7)
                                .addComponent(txtNom, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(addPanLayout.createSequentialGroup()
                                .addComponent(lblEmail)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(addPanLayout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(addPanLayout.createSequentialGroup()
                                .addComponent(lblTel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtTel, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addPanLayout.createSequentialGroup()
                        .addComponent(btVider)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btOk))))
        );
        addPanLayout.setVerticalGroup(
            addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addPanLayout.createSequentialGroup()
                .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addPanLayout.createSequentialGroup()
                        .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNom)
                            .addComponent(txtNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblEmail)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(addPanLayout.createSequentialGroup()
                        .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTel)
                            .addComponent(txtTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(addPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btOk)
                    .addComponent(btVider))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(addPan, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                    .addComponent(clientsTable, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addPan, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(clientsTable, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                .addContainerGap())
        );

        clientsTable.addClientSelectedListener(this);
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == btOk) {
                ClientsPan.this.btOkActionPerformed(evt);
            }
            else if (evt.getSource() == btVider) {
                ClientsPan.this.btViderActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void btViderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btViderActionPerformed
        txtPrenom.setText("");
        txtPrenom.setBackground(Color.WHITE);
        txtNom.setText("");
        txtNom.setBackground(Color.WHITE);
        txtEmail.setText("");
        txtEmail.setBackground(Color.WHITE);
        txtTel.setText("");
        txtTel.setBackground(Color.WHITE);
        btOk.setText("Ajouter");
        editingClient = null;
    }//GEN-LAST:event_btViderActionPerformed

    private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed
        String  nom     = txtNom.getText().trim(), 
                prenom  = txtPrenom.getText().trim(), 
                tel     = txtTel.getText().trim(), 
                email   = txtEmail.getText().trim();
        
        Boolean valid = true;
        
        if("".equals(nom)){
            txtNom.setBackground(Color.red);
            valid = false;
        }
        
        if("".equals(prenom)){
            txtPrenom.setBackground(Color.red);
            valid = false;
        }
        
        //@todo verifier longueur tel et format email
        
        if(valid){
            Client cli;
            if(this.editingClient == null){
                Tools.debug("Création client");
                cli = new Client();
            } else {
                cli = new Client(editingClient.getId());
            }
            cli.setNom(nom);
            cli.setPrenom(prenom);
            cli.setEmail(email);
            cli.setTel(tel);
            cli.setCreated(Calendar.getInstance().getTime());
            if(cli.getId() != null){
                registre.updateClient(editingClient, cli);
            } else {
                registre.saveClient(cli);
            }
            btViderActionPerformed(null);
        }
        
    }//GEN-LAST:event_btOkActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel addPan;
    private javax.swing.JButton btOk;
    private javax.swing.JButton btVider;
    private app.views.ClientsGui clientsTable;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblNom;
    private javax.swing.JLabel lblTel;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNom;
    private javax.swing.JTextField txtPrenom;
    private javax.swing.JTextField txtTel;
    // End of variables declaration//GEN-END:variables

    
    protected void edit(Client cli){
        editingClient = cli;
        
        txtNom.setText(cli.getNom());
        txtPrenom.setText(cli.getPrenom());
        txtTel.setText(cli.getTel());
        txtEmail.setText(cli.getEmail());
        btOk.setText("Modifier");
    }
    
    public ClientsGui getClientsGui(){
        return clientsTable;
    }

    @Override
    public void clientSelected(Client cli) {
        edit(cli);
    }
}
